package contact.domain;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class DAOPhoneNumber {
	SessionFactory sessionFactory;
	Session session;

	public DAOPhoneNumber(){
		try{
			sessionFactory = new Configuration().configure().buildSessionFactory();
			session = sessionFactory.openSession();
		} catch(Exception e){
			System.out.println("ERROR : "+e.getMessage());
		}
	}
	
	public Long create(PhoneNumber phone){
		Transaction transaction = session.beginTransaction();
		Long ret =  (Long) session.save(phone);
		transaction.commit();
		System.out.println(phone.getPhoneKind());
		System.out.println(phone.getPhoneNumber());
		return ret;
	}
	
	public PhoneNumber read(Long id){
		return (PhoneNumber) session.get(PhoneNumber.class, id);
	}
	
	public void update(PhoneNumber phone){
		Transaction transaction = session.beginTransaction();
		session.update(phone);
		transaction.commit();
	}
	
	public void delete(PhoneNumber phone){
		Transaction transaction = session.beginTransaction();
		session.delete(phone);
		transaction.commit();
	}
}
